package com.example.checkqufuface.Customer;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.checkqufuface.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.Adapter.SearchResultAdapter;
import com.example.checkqufuface.R;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.bean.QufuPeopleCardSaleListRequsetBean;
import com.example.checkqufuface.bean.QufuPeopleCardSaleListResultBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SearKaDialog extends BottomDialogBase implements View.OnClickListener {

    private Context context;


    private EditText searchName;
    private EditText searchId;
    private TextView search;
    private RecyclerView rlSearch;
    private List<String> bindList;
    private final SearchResultAdapter searchResultAdapter;
    private QufuPeopleCardSaleListRequsetBean cardSaleListRequsetBean;
    private List<QufuPeopleCardSaleListResultBean.DataBean.DataListBean> dataList;
    private ImageView close;


    public SearKaDialog(final Context context, String cName, final String cId, final String baseUrl, final Map<String, String> headers) {
        super(context);
        this.context = context;

        searchName.setText(cName);
        searchId.setText(cId);

        if (cardSaleListRequsetBean == null) {
            cardSaleListRequsetBean = new QufuPeopleCardSaleListRequsetBean();
        }

        if (dataList == null) {
            dataList = new ArrayList<>();
        }

        cardSaleListRequsetBean.setHolderName(cName);
        cardSaleListRequsetBean.setCertificateNo(cId);

        searchResultAdapter = new SearchResultAdapter(context, dataList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        rlSearch.setLayoutManager(linearLayoutManager);
        rlSearch.setAdapter(searchResultAdapter);

        // 进来就查询一次
        if (!StringUtil.isNullOrEmpty(searchName.getText().toString()) && !StringUtil.isNullOrEmpty(searchName.getText().toString())) {
            headers.clear();
            String token = (String) SPUtils.get(context, Customer.QUFU_ACCESSTOKEN, "");
            if (!StringUtil.isNullOrEmpty(token)) {
                headers.put("Token", token);
            }
            // 去网络请求数据，然后更新数据
            RetrofitClient.getInstance(context, baseUrl, headers).findPeopleCardSaleList(cardSaleListRequsetBean, new BaseSubscriber<QufuPeopleCardSaleListResultBean>(context) {
                @Override
                public void onError(ExceptionHandle.ResponeThrowable e) {
                    SelfToastUtil.getInstance().show(e.message);
                }

                @Override
                public void onNext(QufuPeopleCardSaleListResultBean qufuPeopleCardSaleListResultBean) {
                    String code = qufuPeopleCardSaleListResultBean.getCode();
                    if (code.equals("200")) {

                        dataList.clear();
                        if (qufuPeopleCardSaleListResultBean.getData() != null) {
                            dataList.addAll(qufuPeopleCardSaleListResultBean.getData().getDataList());
                            if (dataList.size() <= 0) {
                                SelfToastUtil.getInstance().show("查询数据列表为空，请重新确认");
                            }
                            searchResultAdapter.notifyDataSetChanged();
                        } else {
                            SelfToastUtil.getInstance().show("查询数据列表为空，请重新确认");
                        }
                    } else {
                        SelfToastUtil.getInstance().show(qufuPeopleCardSaleListResultBean.getMessage());
                    }
                }
            });
        }

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否有数据
                if (StringUtil.isNullOrEmpty(searchName.getText().toString())) {
                    SelfToastUtil.getInstance().show("姓名为空，请先输入姓名");
                    return;
                }

                if (StringUtil.isNullOrEmpty(searchId.getText().toString())) {
                    SelfToastUtil.getInstance().show("证件号码为空，请先输入证件号码");
                    return;
                }

                cardSaleListRequsetBean.setCertificateNo(searchName.getText().toString());
                cardSaleListRequsetBean.setCertificateNo(searchId.getText().toString());
                // 去网络请求数据，然后更新数据
                RetrofitClient.getInstance(context, baseUrl, headers).findPeopleCardSaleList(cardSaleListRequsetBean, new BaseSubscriber<QufuPeopleCardSaleListResultBean>(context) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        SelfToastUtil.getInstance().show(e.message);
                    }

                    @Override
                    public void onNext(QufuPeopleCardSaleListResultBean qufuPeopleCardSaleListResultBean) {
                        String code = qufuPeopleCardSaleListResultBean.getCode();
                        if (code.equals("200")) {
                            dataList.clear();
                            if (qufuPeopleCardSaleListResultBean.getData() != null) {
                                dataList.addAll(qufuPeopleCardSaleListResultBean.getData().getDataList());
                                searchResultAdapter.notifyDataSetChanged();
                            } else {
                                SelfToastUtil.getInstance().show(qufuPeopleCardSaleListResultBean.getMessage() + "，数据列表为空");
                            }
                        } else {
                            SelfToastUtil.getInstance().show(qufuPeopleCardSaleListResultBean.getMessage());
                        }
                    }
                });
            }
        });

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    protected void onCreate() {
        setContentView(R.layout.widget_search_ka);

        // 查找控件
        searchName = findViewById(R.id.tv_widget_bind_name);
        searchId = findViewById(R.id.tv_widget_ka_number);
        search = findViewById(R.id.popup_search_infor);
        rlSearch = findViewById(R.id.rl_tv_search_selete);
        close = findViewById(R.id.iv_wg_qu_xiao_ka);

    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

}


